unit FundsEditFrm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinOffice2013White, dxSkinscxPCPainter,
  cxContainer, cxEdit, dxLayoutcxEditAdapters, dxLayoutControlAdapters,
  Vcl.ComCtrls, dxCore, cxDateUtils, Vcl.Menus, Vcl.StdCtrls, cxButtons, cxMemo,
  cxSpinEdit, cxDropDownEdit, cxCalendar, cxDBExtLookupComboBox,
  dxLayoutContainer, cxTextEdit, cxMaskEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, dxLayoutLookAndFeels, dxLayoutControl, Data.DB,
  FIBDataSet, pFIBDataSet;

type
  TFundsEditForm = class(TForm)
    grp1: TdxLayoutGroup;
    lc1: TdxLayoutControl;
    dxlytlkndflst1: TdxLayoutLookAndFeelList;
    dxlytcxlkndfl1: TdxLayoutCxLookAndFeel;
    itmlc1Item1: TdxLayoutItem;
    cbb_KONTRAGENT: TcxLookupComboBox;
    cbb_DOG: TcxExtLookupComboBox;
    itmlc1Item2: TdxLayoutItem;
    dt_DATA: TcxDateEdit;
    itmlc1Item11: TdxLayoutItem;
    spin_NACH: TcxSpinEdit;
    itmlc1Item12: TdxLayoutItem;
    spin_OPL: TcxSpinEdit;
    itmlc1Item13: TdxLayoutItem;
    mm_NOTE: TcxMemo;
    itmlc1Item14: TdxLayoutItem;
    grplc1Group1: TdxLayoutGroup;
    btnCancel: TcxButton;
    itmlc1Item15: TdxLayoutItem;
    btnSave: TcxButton;
    itmlc1Item16: TdxLayoutItem;
    procedure cbb_KONTRAGENTPropertiesEditValueChanged(Sender: TObject);
    procedure cbb_KONTRAGENTPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure cbb_DOGPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure dt_DATAPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }

    isNewRec: Boolean;
    procedure PrepareDS;
    procedure FillControlls;
    procedure ReopenDog;
    procedure AddRec(isNew: Boolean);
    function Validate: Boolean;
  public
    { Public declarations }
    DS: TpFIBDataSet;
    constructor Create(AOwner: TComponent; dsOut: TpFIBDataSet; isNew: Boolean = false);
  end;

var
  FundsEditForm: TFundsEditForm;

implementation

{$R *.dfm}

uses cmDBUnit;

procedure TFundsEditForm.AddRec(isNew: Boolean);
begin
  if not Validate then Exit;
  with DS do begin
    if isNew then Append else Edit;
      FN('KONTRAGENT').AsVariant:=cbb_KONTRAGENT.EditValue;
      FN('DOG').AsVariant:=cbb_DOG.EditValue;
      FN('DATA').AsVariant:=dt_DATA.EditValue;
      FN('NACH').AsVariant:=spin_NACH.EditValue;
      FN('OPL').AsVariant:=spin_OPL.EditValue;
      FN('NOTE').AsVariant:=mm_NOTE.EditValue;
    Post;
  end;
end;

procedure TFundsEditForm.btnSaveClick(Sender: TObject);
begin
  AddRec(isNewRec);
  ModalResult:= mrOk;
end;

procedure TFundsEditForm.cbb_DOGPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= cbb_DOG.EditValue = null;
end;

procedure TFundsEditForm.cbb_KONTRAGENTPropertiesEditValueChanged(
  Sender: TObject);
begin
  ReopenDog;
end;

procedure TFundsEditForm.cbb_KONTRAGENTPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= cbb_KONTRAGENT.EditValue = null;
end;

constructor TFundsEditForm.Create(AOwner: TComponent; dsOut: TpFIBDataSet;
  isNew: Boolean);
begin
  inherited Create(AOwner);
  PrepareDS;
  DS:= dsOut;
  isNewRec:= isNew;
  if not isNew then FillControlls;
end;

procedure TFundsEditForm.dt_DATAPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= dt_DATA.EditValue = null;
end;

procedure TFundsEditForm.FillControlls;
begin
  with DS do begin
    cbb_KONTRAGENT.EditValue:= FN('KONTRAGENT').AsVariant;
    cbb_DOG.EditValue:= FN('DOG').AsVariant;
    dt_DATA.EditValue:= FN('DATA').AsVariant;
    spin_NACH.EditValue:= FN('NACH').AsVariant;
    spin_OPL.EditValue:= FN('OPL').AsVariant;
    mm_NOTE.EditValue:= FN('NOTE').AsVariant;
  end;
end;

procedure TFundsEditForm.PrepareDS;
begin
  cmDb.ds_KONTRAGENT.Open;
end;

procedure TFundsEditForm.ReopenDog;
begin
  with cmDb.ds_DOG do begin
    Close;
    MainWhereClause:='D.KONTRAGENT = :kontr';
    ParamByName('kontr').AsVariant:= cbb_KONTRAGENT.EditValue;
    Open;
  end;
end;

function TFundsEditForm.Validate: Boolean;
begin
  Result:= False;
  if not cbb_KONTRAGENT.ValidateEdit(False) then Exit;
  if not dt_DATA.ValidateEdit(false) then Exit;
  if not cbb_DOG.ValidateEdit(false) then Exit;
  Result:= True;
end;

end.
